﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Common.Utilities.DLR;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable][OrmClass("WebRequest","WebRequestID",false)]
    public class WebRequest
    {
        private Guid _WebRequestID;
        /// <summary></summary>
        public Guid WebRequestID
        {
            get
            {
                return this._WebRequestID;
            }
            set
            {
                this._WebRequestID = value;
            }
        }

        private WebRequestProtocol _Protocol;
        /// <summary></summary>
        public WebRequestProtocol Protocol
        {
            get
            {
                return this._Protocol;
            }
            set
            {
                this._Protocol = value;
            }
        }

        private string _BaseUrl;
        /// <summary></summary>
        public string BaseUrl
        {
            get
            {
                return this._BaseUrl;
            }
            set
            {
                this._BaseUrl = value;
            }
        }

        private string _RequestPagePath;
        /// <summary></summary>
        public string RequestPagePath
        {
            get
            {
                return this._RequestPagePath;
            }
            set
            {
                this._RequestPagePath = value;
            }
        }

        private List<WebParameter> _Parameters;
        /// <summary></summary>
        public List<WebParameter> Parameters
        {
            get
            {
                return this._Parameters;
            }
            set
            {
                this._Parameters = value;
            }
        }

        private NetworkCredential _Credential;
        /// <summary></summary>
        public NetworkCredential Credential
        {
            get
            {
                return this._Credential;
            }
            set
            {
                this._Credential = value;
            }
        }

        private List<Cookie> _Cookies;
        /// <summary></summary>
        public List<Cookie> Cookies
        {
            get
            {
                return this._Cookies;
            }
            set
            {
                this._Cookies = value;
            }
        }

        private HttpMethod _Method;
        /// <summary></summary>
        public HttpMethod Method
        {
            get
            {
                return this._Method;
            }
            set
            {
                this._Method = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetPostedContent()
        {
            if(this.Method==HttpMethod.POST)
            {
                StringBuilder buffer=new StringBuilder();
                if(this.Parameters !=null && this.Parameters.Count>0)
                {
                    foreach(WebParameter webParam in this.Parameters)
                    {
                        if(webParam.ParemeterType !=WebParameterType.Url)
                        {
                            if(buffer.Length>0)
                            {
                                buffer.Append("&");
                            }
                            buffer.Append(webParam.ParameterName + "=" + webParam.ParameterValue);
                        }
                    }
                }
                return buffer.ToString();
            }
            else
                return string.Empty;
        }

        /// <summary></summary>
        public enum HttpMethod
        {
            /// <summary></summary>
            GET,
            /// <summary></summary>
            POST,
            /// <summary></summary>
            REST
        }
    }
}
